package com.hxzy.projectssmplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.projectssmplus.entity.SysMenu;
import com.hxzy.projectssmplus.mapper.SysMenuMapper;
import com.hxzy.projectssmplus.service.SysMenuService;
import com.hxzy.projectssmplus.vo.RoleTreeVO;
import com.hxzy.projectssmplus.vo.TreeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {


    @Autowired
    private SysMenuMapper sysMenuMapper;

    /**
     * 根据用户id查询它拥有的角色的菜单权限(menu中perms)
     *
     * @param adminUserId
     * @return
     */
    @Override
    public Set<String> ownerMenuByUserId(Integer adminUserId) {
        return this.sysMenuMapper.ownerMenuByUserId(adminUserId);
    }

    @Override
    public List<SysMenu> selectMenuList(SysMenu sysMenu) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(sysMenu.getMenuName()))
            queryWrapper.like("menu_name", sysMenu.getMenuName());
        if (StringUtils.isNotBlank(sysMenu.getVisible()))
            queryWrapper.eq("visible", sysMenu.getVisible());
        if (StringUtils.isNotBlank(sysMenu.getStatus()))
            queryWrapper.eq("status", sysMenu.getStatus());

        return super.list(queryWrapper);
    }

    @Override
    public List<TreeVO> GetTreeSelect(SysMenu sysMenu) {
        //查询所有菜单
        List<SysMenu> allList = this.selectMenuList(sysMenu);

        //返回树
        List<TreeVO> bulidList = new ArrayList<>();

        allList.stream().filter(p -> p.getParentId() == 0L).forEach(p -> {
            TreeVO parent = new TreeVO();
            parent.setMenuId(p.getMenuId());
            parent.setMenuName(p.getMenuName());

            //查询子级是否还有下级
            getChildTree(allList, parent);
            //添加子级
            bulidList.add(parent);
        });
        return bulidList;
    }

//        this.redisTemplate.opsForValue().set(GlobalConst.MENU_TREE_REDIS_PREF,GlobalConst.MENU_TREE_REDIS_PREF);

    @Override
    public List<RoleTreeVO> getTreeByRoleId(Long roleId) {
        List<SysMenu> allList = this.sysMenuMapper.getTreeByRoleId(roleId);
        List<RoleTreeVO>  bulidList= new ArrayList<>();

        allList.stream().filter(p -> p.getParentId() == 0L).forEach(p -> {
            RoleTreeVO parent = new RoleTreeVO();
            parent.setId(p.getMenuId());
            parent.setLabel(p.getMenuName());

            //查询子级是否还有下级
            getRoleChildTree(allList, parent);
            //添加子级
            bulidList.add(parent);
        });
        return bulidList;
    }

    public void getRoleChildTree(List<SysMenu> allList, RoleTreeVO parent) {
        List<RoleTreeVO> buildList = new ArrayList<>();
        allList.stream().filter(p -> p.getParentId() != 0L && p.getParentId() == parent.getId().longValue())
                .forEach(p -> {
                    RoleTreeVO child = new RoleTreeVO();
                    child.setId(p.getMenuId());
                    child.setLabel(p.getMenuName());

                    getRoleChildTree(allList, child);

                    buildList.add(child);

                    if (buildList.size() > 0) {
                        parent.setChildren(buildList);
                    }
                });
    }

    public void getChildTree(List<SysMenu> allList, TreeVO parent) {
        List<TreeVO> buildList = new ArrayList<>();
        allList.stream().filter(p -> p.getParentId() != 0L && p.getParentId() == parent.getMenuId().longValue())
                .forEach(p -> {
                    TreeVO child = new TreeVO();
                    child.setMenuId(p.getMenuId());
                    child.setMenuName(p.getMenuName());

                    getChildTree(allList, child);

                    buildList.add(child);

                    if (buildList.size() > 0) {
                        parent.setChildren(buildList);
                    }
                });
    }
}
